<?php
/**
 * Processes the "remember me" cookie.
 * 
 * @package    symfony
 * @subpackage plugin
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfGuardBasicSecurityFilter.class.php 15757 2009-02-24 21:15:40Z Kris.Wallsmith $
 *  
 */
class sfCMSSecurityFilter extends sfBasicSecurityFilter
{
    /**
   * @see sfFilter
   */
    public function execute($filterChain)
    {
        $sf_user = $this->context->getUser();
        
       
        if( $sf_user->getCulture() != 'ar' &&  $sf_user->getCulture()!='en' && $sf_user->getCulture()!='fa' ){
            $sf_user->setCulture( sfConfig::get('sf_default_culture','en') );
        }

        if (
        $this->isFirstCall()
        &&
        $this->getContext()->getActionName() != sfConfig::get('sf_secure_action','secure')
        &&
        $this->getContext()->getModuleName() != sfConfig::get('sf_secure_module','default')
        &&
        $this->getContext()->getActionName() != sfConfig::get('sf_login_action','login')
        &&
        $this->getContext()->getModuleName() != sfConfig::get('sf_login_module','default')
        )
        {


            //$sf_user->getCulture();



            /*@var $sf_user myUser*/
            $intertnal_uri = $this->getContext()->getRouting()->getCurrentInternalUri(true);
            $alias_url = ltrim(strstr($intertnal_uri,'alias_url='),'alias_url=');

            $menu_item = MenuItemPeer::retrieveByAliasUrl($alias_url);

            if($menu_item){
                $link_perms = $menu_item->getMenuItemPermissions();
            }

            $can_go = true;

            if($menu_item && count($link_perms) > 0){

                if($sf_user->isAnonymous()){
                    $can_go = false;

                }else{

                    /*@var $link MenuItemPermission*/
                    foreach ($link_perms as $link){
                        $perm_name = $link->getsfGuardPermission()->getName();

                        if($sf_user->hasCredential($perm_name)){
                            $can_go = true;
                            break;
                        }
                    }


                }
            }


            if(!$can_go ){

                //$this->getContext()->getController()->redirect(sfConfig::get('sf_login_module').'/'.sfConfig::get('sf_login_action'));
                $this->getContext()->getController()->forward(sfConfig::get('sf_login_module'),sfConfig::get('sf_login_action'));
                die('');
                return ;

            }


        }
        $filterChain->execute();
        //parent::
    }
}
